#include "libFUSE.h"
#include "libExtended2.h"

//static staticGroupDescriptor_t* staticGroupDescriptorTable;
static staticSuperBlock_t staticSuperBlock = { 0 };

struct fuse_operations fs_operations = { .create = &fs_create, .open = &fs_open, .read = &fs_read, .write = &fs_write, .release = &fs_release, .ftruncate = &fs_ftruncate, .unlink = &fs_unlink, .mkdir = &fs_mkdir, .readdir = &fs_readdir, .rmdir = &fs_rmdir, .getattr = &fs_getattr, };

uint32_t fs_create(const char *path, mode_t modo, struct fuse_file_info *fInfo) {
	printf("FS_CREATE_DUMMY\n");
	return 1;
}

uint32_t fs_open(const char *path, struct fuse_file_info* fInfo) {
	abrirArchivo((char* )path);
	printf("FS_OPEN_DUMMY\n");
	return 1;
}

uint32_t fs_read(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fInfo) {
	printf("FS_READ_DUMMY\n");
	return 1;
}

uint32_t fs_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fInfo) {
	printf("===============================================================================\n");
	printf("                                .-------.\n");
	printf("                                | -----.-----.\n");
	printf("                                | -----| ----|\\\n");
	printf("                                | -----| ----- |\n");
	printf("                                | -----| ----- |\n");
	printf("                                '------| ----- |\n");
	printf("                                       '-------'\n\n");
	printf("                                      WRITE\n");
	printf("-------------------------------------------------------------------------------\n");

	inodeStructure_t inodo;
	uint8_t numBlockGroup;
	uint32_t numInodo;

	uint8_t error = obtenerInodoPath((char*) path, &inodo, &numBlockGroup, &numInodo);

	if (!error) {
		if (size + offset > inodo.size) {
			fs_ftruncate(path, offset, fInfo);
		}

		div_t divInicio = div(offset, staticSuperBlock.blockSize);
		uint64_t bloqueInicio = divInicio.quot;

		div_t divFin = div(offset + size, staticSuperBlock.blockSize);
		uint64_t bloqueFin = divFin.quot;

		printf("Primer bloque: [%d] \n", (uint32_t) bloqueInicio);
		printf("Último bloque: [%d] \n", (uint32_t) bloqueFin);

		if (bloqueInicio < 12 && bloqueFin < 12) {

		}

		//se redondea el size para abajo y el offset para arriba para leer bloques completos
		//se leen los bloques completos (solo los que haya que escribir)
		//se pisa la informacion a escribir
		//se escriben los bloques necesarios

		return 0;
	} else {
		return -ENOENT;
	}
	return 0;
}

uint32_t fs_release(const char *path, struct fuse_file_info *fInfo) {
	printf("FS_RELEASE_DUMMY\n");
	return 1;
}

uint32_t fs_ftruncate(const char *path, off_t offset, struct fuse_file_info *fInfo) {
	printf("FS_FTRUNCATE_DUMMY\n");
	return 1;
}

uint32_t fs_unlink(const char *path) {
	printf("FS_UNLINK_DUMMY\n");
	return 1;
}

uint32_t fs_mkdir(const char *path, mode_t modo) {
	printf("FS_MKDIR_DUMMY\n");
	return 1;
}

uint32_t fs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fInfo) {
	printf("FS_READDIR_DUMMY\n");
	return 1;
}

uint32_t fs_rmdir(const char *path) {
	printf("FS_RMDIR_DUMMY\n");
	return 1;
}

uint32_t fs_getattr(const char* path, struct stat* stbuf) {
	printf("FS_GETATTR_DUMMY\n");
	return 1;
}
